package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 视频上传
     *
     * @param videoThumbnail：视频封面
     * @param videoFile：视频
     * @return
     */
    @PostMapping
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videoService.uploadVideo(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity videoList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = videoService.videoList(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id) {
        videoService.videoLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频取消点赞
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id) {
        videoService.videoDislike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 关注视频用户
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid) {
        System.out.println(uid);
        videoService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注视频用户
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid) {
        videoService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论发布
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String id, @RequestBody Map map) {
        String comment = (String) map.get("comment");
        videoService.comments(id, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String id, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = videoService.commentsList(id, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 对视频里的评论点赞
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentLike(@PathVariable("id") String id){
        videoService.commentsLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 对视频里的评论点赞
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentDislike(@PathVariable("id") String id){
        videoService.commentsDislike(id);
        return ResponseEntity.ok(null);
    }
}
